<?php

namespace app\backstage\controller;

use think\Controller;
use think\Request;
use app\backstage\model\SystemSettings as SetingModel;
use app\backstage\model\HouseModule;


class Systemsettings extends Controller
{
    /**
     * 系统设置
     *
     * @return \think\Response
     */
    public function index(Request $request, SetingModel $model)
    {
        $info = $model->find();


        if($request->isAjax()){
            $data = $request->param();

//            dump($data);die;

            $res = $model->allowField(true)->save($data,['id'=>1]);

            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>100];
            }
        }
        return view('index',['info'=>$info]);
    }

    /**
     * 模块设置
     *
     * @return \think\Response
     */
    public function house_module(Request $request, HouseModule $model)
    {
        $info = $model->select();

        
        if($request->isAjax()){

            if($request->isGet()){
                $modules = $resoldModule = $newModule =[];

                if($info){
                    foreach($info as $k=>$v){
                        if($v['type']==1){
                            array_push($newModule,$v);
                        }elseif($v['type']==2){
                            array_push($resoldModule,$v);

                        }else{
                            array_push($modules,$v);
                        }
                    }
                }

                return ['newModule'=>$newModule,'resoldModule'=>$resoldModule,'modules'=>$modules];
            }else{
                $data = $request->param();

                if($data){
                    foreach($data as $k=>$v){
                        if(is_array($v)){
                            foreach($v as $vv) {

                                $res = $model->where('id', $vv['id'])->update($vv);
                            }
                        }

                    }
                }

//                if($res){

                    return ['code'=>200];
//                }else{
//                    return ['code'=>100];
//                }
            }

        }




        return view('housemodule');
    }


}
